﻿using BootstrapBlazor.Components;
using CodeMan.Application.Contracts.ClassGens;
using CodeMan.ClassGens;
using CodeManDesktopBlazor.BlazorComponents.Pages;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Components.WebView.Wpf;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Volo.Abp.Data;
using Volo.Abp.ObjectMapping;

namespace CodeManDesktopBlazor
{


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private readonly HelloWorldService _helloWorldService;
        protected override void OnContentRendered(EventArgs e)
        {

        }

        public MainWindow(HelloWorldService helloWorldService)
        {
            try
            {

                _helloWorldService = helloWorldService;

                //DemoToDo();

                CheckBlazorUI();
                InitializeComponent();
                this.initWindowStateStartupLocation();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.CursorArrow();
            }
        }
        public void Show(string Title)
        {
            this.Title = Title;
            this.Show();
        }
        public void DoItAsync2(string strname)
        {
            var dd = _helloWorldService.DoItAsync2(strname);
            var c = 1;
        }

        public async Task DemoToDo()
        {
            await _helloWorldService.Demotodo();
            var c = 1;
        }

        #region Get Data Service
        public IConnectionStringResolver GetconnectionStringResolver()
        {
            return _helloWorldService.GetconnectionStringResolver();
        }
        public IOtherDBAppService GetOtherDBAppService()
        {
            return _helloWorldService.GetOtherDBAppService();
        }

        public IClassGenAppService GetClassGenAppService()
        {
            return _helloWorldService.GetClassGenAppService();
        }
        public IDataService<ClassGenDTOCreateUpdate> GetDataService()
        {
            return _helloWorldService.GetDataService();
        }
        public IDataService<ClassAttributeDTO> GetDataDetailsMainService()
        {
            return _helloWorldService.GetDataDetailsMainService();
        }
        public IDataDetailService<ClassAttributeDTO> GetDataDetailsService()
        {
            return _helloWorldService.GetDataDetailsService();
        }
        public IObjectMapper GetobjectMapper()
        {
            return _helloWorldService.GetobjectMapper();
        }

        #endregion
        void CheckBlazorUI()
        {
            try
            {
                if (!CheckWebView())
                {
                    MessageBox.Show("检测到当前未安装运行环境，正在启动安装程序，请稍后...");
                    InstallWebView();
                    Stopwatch.StartNew();
                    while (!CheckWebView())
                    {
                        Thread.Sleep(3000);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private static bool CheckWebView()
        {
            try
            {
                var str = CoreWebView2Environment.GetAvailableBrowserVersionString();
                if (!string.IsNullOrWhiteSpace(str))
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }
        private static void InstallWebView()
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + "check";

            using (Process process = new Process())
            {
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = path + "\\MicrosoftEdgeWebview2Setup.exe";
                try
                {
                    process.Start();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
